#include "Line_custom.h"
Line_custom::Line_custom(IDirect3DDevice9* Device) {
	 
	_device = Device;
 
	InitD3DVertexBuffer();
}
void Line_custom::InitD3DVertexBuffer() {
	CUSTOMVERTEX vertices[4] =
	{
		/*{50.0f,50.0f,1.0f },
		{50.0f,250.0f,1.0f },
		{250.0f,50.0f,1.0f },
		{250.0f,250.0f,1.0f }*/
		{0.0f,0.0f,0.0f },
		{50.0f,250.0f,1.0f},
		{250.0f,50.0f,1.0f  },
		{250.0f,250.0f,1.0f }
	};


	//create vertex buffer.
	/*_device->CreateVertexBuffer(
		4*sizeof(CUSTOMVERTEX),
		0,
		D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT,
		&g_pd3dVB,
		NULL);*/
	_device->CreateVertexBuffer(
		sizeof(vertices),
		0,
		D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT,
		&g_pd3dVB,
		NULL);
	//fill vertex buffer.
	void* pVertices = NULL;
	g_pd3dVB->Lock(0, sizeof(vertices), &pVertices, 0);


	memcpy(pVertices, vertices, sizeof(vertices));

	g_pd3dVB->Unlock();
}

Line_custom::Line_custom(IDirect3DDevice9* Device, CUSTOMVERTEX * vertices,int size) {
	_size = size;
	_device = Device;

	InitD3DVertexBuffer(vertices,size);
}

 
void Line_custom::InitD3DVertexBuffer(CUSTOMVERTEX * vertices,int size) {
	//CUSTOMVERTEX vertices[4] =
	//{
	//	/*{50.0f,50.0f,1.0f },
	//	{50.0f,250.0f,1.0f },
	//	{250.0f,50.0f,1.0f },
	//	{250.0f,250.0f,1.0f }*/
	//	{0.0f,0.0f,0.0f },
	//	{50.0f,250.0f,1.0f},
	//	{250.0f,50.0f,1.0f  },
	//	{250.0f,250.0f,1.0f }
	//};


	//create vertex buffer.
	/*_device->CreateVertexBuffer(
		4*sizeof(CUSTOMVERTEX),
		0,
		D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT,
		&g_pd3dVB,
		NULL);*/
	_device->CreateVertexBuffer(
		size *sizeof(CUSTOMVERTEX),
		0,
		D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT,
		&g_pd3dVB,
		NULL);
	//fill vertex buffer.
	void* pVertices = NULL;
	g_pd3dVB->Lock(0, size * sizeof(CUSTOMVERTEX), &pVertices, 0);


	memcpy(pVertices, vertices, size * sizeof(CUSTOMVERTEX));

	g_pd3dVB->Unlock();
}

void Line_custom::Render() {

	D3DXMATRIX lw;
	D3DXMatrixTranslation(&lw, 0.0f,0.0f,0.0f);
	_device->SetTransform(D3DTS_WORLD, &lw);
	_device->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
	_device->SetStreamSource(0, g_pd3dVB, 0, sizeof(CUSTOMVERTEX));
	_device->SetFVF(D3DFVF_CUSTOMVERTEX);
	_device->DrawPrimitive(D3DPT_LINELIST, 0, (_size/2));
	_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	 
}